package com.boc.address.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.boc.address.R;
import com.boc.address.R2;
import com.boc.address.adapter.AddressListAda;
import com.boc.address.bean.AddressListBean;
import com.boc.address.contract.AddressListContract;
import com.boc.address.presenter.AddressListPresenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import demo.bocweb.com.sdk.base.BasePresenter;
import demo.bocweb.com.sdk.base.activity.BaseMVPActivity;
import demo.bocweb.com.sdk.config.ARoutConfig;
import demo.bocweb.com.sdk.manager.MessageEvent;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.ToolbarHelper;

/**
 * 选择地址列表（无删除，编辑功能）
 * Created by zhelun on 2018/2/1.
 */
@Route(path = ARoutConfig.chooseAddressPath)
public class ChooseAddressListActivity extends BaseMVPActivity<AddressListContract.IAddressListPresenter, AddressListContract.IAddressListModel>
        implements AddressListContract.IAddressListView {
    @BindView(R2.id.recycler)
    XRecyclerView recycler;
    private List<AddressListBean> addresses = new ArrayList<>();
    private AddressListAda addressListAda;

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return new AddressListPresenter();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_addresslist;
    }

    @Override
    protected void initData() {
        super.initData();
        ToolbarHelper.init(this, R.string.str_title_address_list, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        }, "添加", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(ARoutConfig.addAddressPath).navigation();
            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setLoadingMoreEnabled(false);
        addressListAda = new AddressListAda(false, this);
        recycler.setAdapter(addressListAda);
        addresses.add(new AddressListBean());
        addresses.add(new AddressListBean());
        addresses.add(new AddressListBean());
        addresses.add(new AddressListBean());
        addressListAda.resetItems(addresses);
        Loading.show(this, "加载中...");
        getAddressList();
        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getAddressList();
            }

            @Override
            public void onLoadMore() {

            }
        });

    }

    private void getAddressList() {
        mPresenter.getAddressList();
    }

    @Override
    public void updateAddressList(List<AddressListBean> beans) {
        addresses.clear();
        addresses.addAll(beans);
        addressListAda.resetItems(addresses);

    }

    @Override
    public void setDefaultSuccess() {

    }

    @Override
    public void deleteSuccess() {

    }

    @Override
    public void delete(int position) {

    }

    @Override
    public void setDefault(int position) {

    }

    @Override
    public void onMainEvent(MessageEvent messageEvent) {
        super.onMainEvent(messageEvent);
        if (messageEvent.getMessage().equals("addAddress")){
            getAddressList();
        }
    }
}
